package de.tum.team26.eistpoll.server;

import java.util.LinkedList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Transactional;

import de.tum.team26.eistpoll.client.PollService;
import de.tum.team26.eistpoll.shared.PollData;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Class to serve the RPC ... for Documentation see Docu of PollService.java
 */
public class PollServiceImpl extends RemoteServiceServlet implements
		PollService {

	LinkedList<PollDS> poll;
	Object monitor = new Object();
	

	public PollServiceImpl() {
		getPersistance();
	}
	
	@Transactional
	private void getPersistance() {
		poll = new LinkedList<PollDS>();
		List<PollDS> temp = PMF.getPolls();
		if (temp == null)
			return;
		for (PollDS e : temp) {
			poll.add(e);
		}
	}

	@Transactional
	public void pushPollData(int id, int option, String like, String dislike) {

		for (PollDS e : poll) {
			if (e.getId() == id) {
				final String ip = getThreadLocalRequest().getRemoteAddr();
				if (!e.checkIP(ip)){
					e.addResult(option, like, dislike);
					e.addIP(ip);
					e.setPersistantData();
				}
			}
		}

		// Notify Clients about the Change
		synchronized (monitor) {
			monitor.notifyAll();
		}

	}

	@Override
	public LinkedList<PollData> getResults(boolean isLecturer) {
		if (poll.isEmpty())
			return null;
		LinkedList<PollData> temp = new LinkedList<PollData>();

		for (PollDS e : poll) {
			temp.add(e.getPollData());
		}

		return temp;
	}

	@Transactional
	public boolean checkUser(String user, String pass) {
		Logins login = new Logins();
		login.setPersistantData();
		login.getPersistantData();
		return login.checkUser(user, pass);
	}

	@Override
	public boolean pushService() {
		int numTemp = 0;
		int numTemp2 = 0;
		for (PollDS e : poll) {
			numTemp = numTemp + e.getNumVotes();
		}
		synchronized (monitor) {
			try {
				monitor.wait(50000);
			} catch (InterruptedException e) {

			}
		}		
		for (PollDS e : poll) {
			numTemp2 = numTemp2 + e.getNumVotes();
		}
		if (numTemp == numTemp2) {
			return false;
		}
		else{
			return true;
		}
	}

	@Transactional
	public void resetStatistic(int id) {
		for (PollDS e : poll) {
			if (e.getId() == id) {
				e.reset();
				e.setPersistantData();
			}
		}
	}

	@Override
	public PollData getActivePoll() {

		PollDS temp = null;

		for (PollDS e : poll) {
			if (e.getIsActive()) {
				temp = e;
				break;
			}
		}

		if (temp != null) {
			// IP Check
			final String ip = getThreadLocalRequest().getRemoteAddr();
			if (temp.checkIP(ip)) {
				PollData temp2 = temp.getPollData();
				temp2.alreadyVoted = true;
				return temp2;
			} else {
				return temp.getPollData();
			}
		} else
			return null;

	}

	@Transactional
	public void deletePoll(int id) {
		// getPersistance();

		PollDS temp = null;

		for (PollDS e : poll) {
			if (e.getId() == id) {
				temp = e;
				break;
			}
		}
		if (temp != null) {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try {
				pm.deletePersistent(pm.getObjectById(PollDS.class, id));
			} catch (javax.jdo.JDOObjectNotFoundException e) {

			} finally {
				pm.close();
			}
		}
		getPersistance();
	}

	@Transactional
	public void switchPollActive(int id) {
		boolean found = false;
		getPersistance();
		for (PollDS e : poll) {
			if (e.getId() == id)
				found = true;
		}
		if (found) {
			for (PollDS e : poll) {
				if (e.getId() == id)
					e.setIsActive(!e.getIsActive());
				else
					e.setIsActive(false);
				e.setPersistantData();
			}
		}
	}

	@Transactional
	public void addPoll(String title) {
		// ID feststellen:

		/*
		 * int i = 1;
		 * 
		 * for (PollDS e : poll) { if (e.getId() >= i) i = e.getId() + 1; }
		 */
		PollDS newPoll = new PollDS();
		// newPoll.setId(i);
		newPoll.setTitle(title);
		newPoll.setPersistantData();

		getPersistance();

	}

}
